package com.hghivln.config.shiro;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2 // 启用swagger2
public class SwaggerConfig {
    /**
     * 在Ioc容器中
     */
    public Docket swaggerSpringMvcPlugin(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select() // 通过select()方法，去配置扫描接口，RequestHandlerSelectors配置如何扫描接口
                .apis(RequestHandlerSelectors.withMethodAnnotation(RestController.class))
                .build();
    }

    private ApiInfo apiInfo(){
        Contact contact = new Contact("Miss黄", "", "1179531613@qq.com");
        return new ApiInfoBuilder()
                .contact(contact)
                .version("v1.0")
                .license("Apach 2.0 许可")
                .build();
    }

    /**
     * 配置doctet 以配置swagger具体参数
     */
    @Bean
    public Docket docket(Environment environment) {
        //添加head参数配置start
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        List<Parameter> operationParameters = new ArrayList<>();
        operationParameters.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2).groupName("group1")
                .apiInfo(apiInfo())
                .enable(true) // 配置是否启用swagger，如果是false，在浏览器无法访问
                .select()   // 通过select()方法，去配置扫描接口，RequestHandlerSelectors配置如何扫描接口
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .build()
                .globalOperationParameters(operationParameters); // swagger加请求头(测试带token的请求)
    }
}
